
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
vite-plugin-resolve
Advanced tools
Custom resolve module content
English | 简体中文
npm i vite-plugin-resolve -D
import { defineConfig } from 'vite'
import resolve from 'vite-plugin-resolve'
export default defineConfig({
plugins: [
resolve({
// Resolve custom module content
// This like Vite external plugin
vue: `const vue = window.Vue; export { vue as default }`,
}),
]
})
resolve({
// Support nested module id
// Support return Promise
'@scope/name': async () => await require('fs').promises.readFile('path', 'utf-8'),
})
resolve({
// Resolve Electron ipcRenderer in Renderer-process
electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`,
})
Such as execa, node-fetch
Here, Vite is used as the build tool
You can also choose other tools, such as rollup, webpack, esbuild, swc and so on
import { builtinModules } from 'module'
import { defineConfig, build } from 'vite'
import resolve from 'vite-plugin-resolve'
export default defineConfig({
plugins: [
resolve({
async execa(args) {
// Transpile execa as an CommonJs module
await build({
plugins: [
{
name: 'vite-plugin[node:mod-to-mod]',
enforce: 'pre',
// Replace `import fs from "node:fs"` with `import fs from "fs"`
resolveId(source) {
if (source.startsWith('node:')) {
return source.replace('node:', '')
}
},
}
],
// Build execa.js into cache directory
build: {
outDir: args.dir,
minify: false,
emptyOutDir: false,
lib: {
entry: require.resolve('execa'),
formats: ['cjs'],
fileName: () => `execa.js`,
},
rollupOptions: {
external: [
...builtinModules,
],
},
},
})
},
})
]
})
export interface Resolves {
[moduleId: string]:
| string
| ((args: ResolveArgs) =>
| string
| Promise<string | void>
| void)
| void;
}
export interface ResolveArgs {
/** Generated file cache directory */
dir: string;
}
export interface ResolveOptions {
/**
* Absolute path or relative path
* @default ".vite-plugin-resolve"
*/
dir: string;
}
resolve({
vue: `const vue = window.Vue; export { vue as default }`,
})
node_modules/.vite-plugin-resolve/vue.js
and contains the following codeconst vue = window.Vue; export { vue as default }
vue
alias item and add it to resolve.alias
{
resolve: {
alias: [
{
find: 'vue',
replacement: 'User/work-directory/node_modules/.vite-plugin-resolve/vue.js',
},
],
},
}
vue
to the optimizeDeps.exclude
by default.optimizeDeps.include
export default {
optimizeDeps: {
exclude: ['vue'],
},
}
FAQs
Custom resolve module content.
The npm package vite-plugin-resolve receives a total of 4,390 weekly downloads. As such, vite-plugin-resolve popularity was classified as popular.
We found that vite-plugin-resolve demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.